﻿{- -}
module Main (main) where 
import System.Directory
import Text.HTML.TagSoup
import Text.Regex.Posix
import Control.Arrow ( (&&&) )

type FSPlain = String
type FSContent = String
data Frameset = Frameset FSPlain FSContent deriving (Show)

type SHLevel = Int
type SHHref  = String
type SHTarget = String
type SHText = String
type SHId = Int
data SAPHelpTreeItem = SAPHelpTreeItem SHLevel SHHref SHTarget SHText SHId deriving (Show, Read)
--data SAPHelpTree = Foldera

walkDir :: String -> IO [FilePath]
walkDir d = do
	cp <- canonicalizePath d
	getDirectoryContents cp

{-Existence tests  
doesFileExist :: FilePath -> IO Bool Source 
The operation doesFileExist returns True if the argument file exists and is not a directory, and False otherwise.  
doesDirectoryExist :: FilePath -> IO Bool Source 
The operation doesDirectoryExist returns True if the argument file exists and is a directory, and False otherwise.  
-}

rootDir = "/usr/local/share/doc/saphelp-nwpi71ehp1/en"
--rootDir = "D:\\SAPHelp.NW71\\PlainHTML\\helpdata\\en"
indexFileName = "index.htm"

findSourceUrl :: String -> String
findSourceUrl s = getUrl . fromAttrib "content" $ (findMetas . parseTags $ s)!!0
    where
        findMetas = filter (~== TagOpen "meta" [("http-equiv", "refresh")])
        getUrl x = drop 4 $ x =~ "URL=(.*)" :: String

getFrameSet :: String -> Frameset
getFrameSet s = Frameset urlTOC urlText
    where
        sapTOC = TagOpen "FRAME" [("NAME", "SAP_TOC")]
        sapText = TagOpen "FRAME" [("NAME", "SAP_TEXT")]
        tags = parseTags s
        url x = fromAttrib "SRC" $ (!!0) $ filter (~== TagOpen "FRAME" [("NAME", x)]) tags
        urlTOC = url "SAP_TOC"
        urlText = url "SAP_TEXT"

parseTree :: String -> [SAPHelpTreeItem]
parseTree s = tagse
    where
        tr = TagOpen "TR" []
        a = TagOpen "A" []
        trc = TagClose "TR"
        tags = map (\x-> takeWhile (~/=trc) x) $ partitions (~== tr) $ parseTags s
        
        tagse = map zz tags
        zz :: [Tag] -> SAPHelpTreeItem
        zz [tr, td, img1, a1, TagText text2, img2, a2, TagClose tdc] = zzi "" a1 text2
        zz [tr, td, TagText text1, img1, a1, TagText text2, img2, a2, TagClose tdc] = zzi text1 a1 text2
        zz [tr, td, TagText text1, img1, a1, imgbook, TagText text2, a2, TagClose a, TagClose tdc] = zzi text1 a1 text2
        zz q = error "Error by parsing in parseTree for id=0"
        zzi l a t = SAPHelpTreeItem (q3 l) (fromAttrib "HREF" a) (fromAttrib "TARGET" a) t 0
            where q3 xs = length xs `quot` 3
    
vizTree :: Int -> String -> [SAPHelpTreeItem] -> String
vizTree i (x:xs) r = ""

main :: IO ()        
main = do
	-- a <- walkDir rootDir
    idx <- readFile (rootDir ++ "/" ++ indexFileName)
    print $ findSourceUrl idx
    fsf <- readFile (rootDir ++ "/00/" ++ "012d30f16d47aa889f63ce996d8544" ++ "/" ++ "frameset.htm")
    print $ getFrameSet fsf
    tree <- readFile "plain.htm" -- (rootDir ++ "\\46\\0714a4e3f34f08e10000000a114a6b\\plain.htm")
    mapM_ print $ parseTree tree
    writeFile "SAPHelpTools.dot" $ vizTree 0 "" $ parseTree tree
        

